package com.linkelee.biz.service.app;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkelee.api.exception.OverAllException;
import com.linkelee.api.model.app.AppUser;
import com.linkelee.api.request.app.AppLoginRequest;
import com.linkelee.api.service.app.IAppUserService;
import com.linkelee.biz.mapper.app.AppUserMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;


@Service("userService")
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {

	@Override
	public AppUser queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<AppUser>().eq("mobile", mobile));
	}

	@Override
	public long login(AppLoginRequest request) {
		AppUser user = queryByMobile(request.getMobile());
		Assert.isNull(user, "手机号或密码错误");
		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(request.getPassword()))){
			throw new OverAllException("手机号或密码错误");
		}
		return user.getUserId();
	}
}
